package com.example.stock.service.impl;

import com.example.stock.controller.dto.YearPctStaDTO;
import com.example.stock.excel.*;
import com.example.stock.model.WeekDetail;
import com.example.stock.service.StockService;
import com.example.stock.service.WeekDataService;
import com.example.stock.service.WuguService;
import com.example.stock.service.YearDataService;
import com.example.stock.util.DataUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class YearDataServiceImpl implements YearDataService {

    WuguService wuguService;

    WeekDataService weekDataService;

    StockService stockService;

    @Autowired
    public void setStockService(@Lazy StockService stockService) {
        this.stockService = stockService;
    }

    @Autowired
    public void setWuguService(WuguService wuguService) {
        this.wuguService = wuguService;
    }

    @Autowired
    public void setWeekDataService(WeekDataService weekDataService) {
        this.weekDataService = weekDataService;
    }

    private List<YearPctStaDTO> yearPctStatList;

    private List<YearPctStat> getRawYearPctStatData() {
        return DataUtil.getYearPctStatData();
    }

    @Override
    public List<YearPctStaDTO> getYearPctStatList() {
        if (yearPctStatList != null) {
            return yearPctStatList;
        }

        Map<String, StockIntroduce> rankMap = stockService.getStockIntroduceList().stream()
                .filter(i -> i.getRank() > 0)
                .collect(Collectors.toMap(StockIntroduce::getStock, item -> item));

        List<YearPctStaDTO> ret = new ArrayList<>();

        getRawYearPctStatData().forEach(yearPctStat -> {
            YearPctStaDTO yearPctStaDTO = new YearPctStaDTO();
            BeanUtils.copyProperties(yearPctStat, yearPctStaDTO);

            Wugu rawWugu = wuguService.getRawWugu(yearPctStat.getStock());
            if (rawWugu != null) {
                yearPctStaDTO.setIndustry(rawWugu.getIndustry());
                yearPctStaDTO.setWg(rawWugu.getWugu());
            }

            WeekDetail currentWeekDetail = weekDataService.getCurrentWeekDetail(yearPctStat.getStock());
            if (currentWeekDetail != null) {
                yearPctStaDTO.setLevelPoint(currentWeekDetail.getLp());
            }

            // rank
            if (rankMap.get(yearPctStat.getStock()) != null) {
                yearPctStaDTO.setRank(rankMap.get(yearPctStat.getStock()).getRank());
            }

            ret.add(yearPctStaDTO);
        });

        yearPctStatList = ret;

        return ret;
    }

    private Map<String, YearPctStat> yearPctStatMap;

    @Override
    public Map<String, YearPctStat> getYearPctStatMap() {
        if (yearPctStatMap != null) {
            return yearPctStatMap;
        }

        yearPctStatMap = getYearPctStatList().stream().collect(Collectors.toMap(YearPctStat::getStock, yearPctStat -> yearPctStat));
        return yearPctStatMap;
    }

    @Override
    public Double getLastYearPct(String stock) {
        return getRawYearPctStatData().stream().collect(Collectors.toMap(YearPctStat::getStock,i -> i)).get(stock).getY2025();
    }

    @Override
    public Map<String, List<MinYearPct>> getMinYearPctMapGroupByStock() {
        return DataUtil.getMinYearPctList().stream().collect(Collectors.groupingBy(MinYearPct::getStock));
    }
}
